﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.shop.shop_product
{
    public partial class product_list : System.Web.UI.Page
    {
        YXShop.BLL.Product.ProductInfo productBll = new YXShop.BLL.Product.ProductInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Session["MemberID"]!=null)
               {

                   if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != "")
                   {
                       string types = Request["Option"].Trim();
                       string id = ChangeHope.WebPage.PageRequest.GetFormString("id");
                       if (types == "del")
                       {
                           productBll.DeleleById(Convert.ToInt32(id));
                       }
                       if (types == "delAll")
                       {
                           productBll.Delete(id);
                       }
                       Response.End();
                       return;
                   }
                   string type = ChangeHope.WebPage.PageRequest.GetQueryString("type");
                   string where = string.Empty;
                   if (type != null && type != "")
                   {
                       switch (type)
                       {
                           case "saleIn":
                               this.lblTitle.Text = "出售中的宝贝";
                               where = " and pro_PutoutID=" + Session["MemberID"] + " and pro_PutoutType=1 and pro_SaleNum=0 ";
                               break;
                           case "saleEnd":
                               this.lblTitle.Text = "已出售的宝贝";
                               where = " and pro_PutoutID=" + Session["MemberID"] + " and pro_PutoutType=1 and pro_SaleNum > 0";
                               break;
                           default:
                               break;
                       }
                       ViewState["where"] = where;
                   }
                   this.litlist.Text = GetList(where);

               }
               
            }
        }

        private string GetList(string where)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = productBll.GetList(" [order by] pro_UpdateTime DESC ", 20, where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("","商品名称");
            table.AddHeadCol("","所属分类");
            table.AddHeadCol("","商城价格");
            table.AddHeadCol("","库存数量");
            table.AddHeadCol("","操作");
            table.AddRow();
            string view = string.Empty;
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {    
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["pro_ID"].ToString() + "\" />");
                    table.AddCol(dataPage.DataReader["pro_Name"].ToString());
                    table.AddCol(GetPuductType(Convert.ToInt32(dataPage.DataReader["cid"])));
                    table.AddCol(dataPage.DataReader["pro_ShopPrice"].ToString());
                    table.AddCol(dataPage.DataReader["pro_Stock"].ToString());
                    //string url = YXShop.TemplateAction.Common.UrlStr("product_info_edit", ", "membercenter/shop/shop_product/");
                    table.AddCol("<a href=\"product_info_edit.aspx?cid=" + dataPage.DataReader["cid"].ToString() + "&id=" + dataPage.DataReader["pro_ID"].ToString()+"\">编辑</a>&nbsp;<a href=\"#\" onclick='Del(" + dataPage.DataReader["pro_ID"] + ")'>删除</a>");
                    table.AddRow();
                }
                view = table.GetTable() + dataPage.PageToolBar;
            }
            else
            {
                view = table.GetTable() + "没有您要的信息";
            }           
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        //商品分类
        private string GetPuductType(int id)
        {
            YXShop.BLL.Product.Productclass classBll = new YXShop.BLL.Product.Productclass();
            YXShop.Model.Product.Productclass model = classBll.GetModelID(id);
            if (model != null)
            {
                return model.Name.ToString();
            }
            else
            {
                return "";
            }

        }

        protected void linkSarch_Click(object sender, EventArgs e)
        {
            this.litlist.Text = GetList(ViewState["where"].ToString());
        }
    }
}
